package chabernac.android.wordbattle;

import chabernac.android.wordbattle.model.WordRaster;

public class SelectedLetterHelper {
  private final WordRaster myRaster;
  
  private int myPreviousX = -1;
  private int myPreviousY = -1;

  public SelectedLetterHelper(WordRaster aRaster) {
    super();
    myRaster = aRaster;
  }
  
  public void down(int x, int y){
    //select all values between the previous coordinates and the new coordinates
    //when the cpu is bussy it might be that some touch events where missed, but because the user
    //did not release his finger he must have passed the missing coordinates
    if(myPreviousX == -1) myPreviousX = x;
    if(myPreviousY == -1) myPreviousY = y;
    
    int theXDelta = getDelta(myPreviousX, x);
    
    int theYDelta = getDelta(myPreviousY, y);
    
    int theSteps = Math.max(Math.abs(x-myPreviousX), Math.abs(y-myPreviousY)) + 1;
    
    int theCurrentX = myPreviousX;
    int theCurrentY = myPreviousY;
    for(int i=0;i<theSteps;i++){
      myRaster.setSelectedLetter(theCurrentX, theCurrentY, true);
      theCurrentX += theXDelta;
      theCurrentY += theYDelta;
    }
    
    myPreviousX = x;
    myPreviousY = y;
  }
  
  private int getDelta(int aX1, int aX2){
    if(aX1 < aX2) return 1;
    if(aX2 < aX1) return -1;
    return 0;
  }
  
  public void up(){
    myPreviousX = -1;
    myPreviousY = -1;
    myRaster.checkForHiddenWord();
  }
  
  

}
